package com.wfl.sampling.service.impl;

import com.wfl.sampling.dao.PostDao;
import com.wfl.sampling.dao.ShadowDao;
import com.wfl.sampling.entity.PostClick;
import com.wfl.sampling.entity.PostDo;
import com.wfl.sampling.entity.PostNew;
import com.wfl.sampling.service.PostService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

import static java.awt.SystemColor.info;
@Slf4j
@Service
public class PostServiceImpl implements PostService {
    @Resource
    private PostDao postDao;
    @Transactional(rollbackFor = { Exception.class })
    @Override
    public void createPost(PostDo post) {
        try {
            postDao.savePost(post);
            postDao.insertPostImage(post);
        } catch (Exception e) {
            log.info("创建失败");
        }
    }

    @Transactional
    @Override
    public List<PostDo> findPost() {
        List<PostDo> postDos = postDao.selectPost();
        PostDo postDo;
        for(int i =0;i<postDos.size();i++){
            postDo = postDos.get(i);
            List<String> imagePaths = postDao.selectPostImages(postDo.getPostId());
            postDo.setImagePaths(imagePaths);
            postDo.setPostNews(postDao.selectPostNews(postDo.getPostId()));
        }
        return postDos;
    }
    @Override
    public void deletePost(Integer postId) {
        postDao.deletePost(postId);
    }

    @Override
    public boolean insertComment(PostNew postNew) {
        postDao.insertComment(postNew);
        return true;
    }

    @Override
    public boolean insertClick(PostClick postClick) {
        PostClick p1 = null;
        p1 = postDao.selectPostClick(postClick);
        if(p1==null) {
            postDao.insertPostClick(postClick);
            int count = postDao.countPostClick(postClick.getPostId());
            postDao.updatePostCountClick(count,postClick.getPostId());
        }
        else {
            postDao.updatePostClick(postClick);
            int count = postDao.countPostClick(postClick.getPostId());
            postDao.updatePostCountClick(count,postClick.getPostId());
        }
        return true;
    }

    @Override
    public List<Integer> getAllClick(Integer id) {
        List<Integer> postIds = postDao.selectPostIdClick(id);
        if (postIds==null)
            return null;
        else return postIds;
    }


}
